#include <QApplication>
#include <QtDebug>
#include <QThreadPool>
#include <iostream>
#include "fastbuffermanager.h"
#include "fastbufferhandler.h"
#include "fasttask.h"

#define THREAD_COUNT 10
#define BUFFER_SIZE 1024

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    FastBufferManager manager;
    manager.setTimeout(1000); // 超时阈值1000ns
    manager.createFastBufferList(BUFFER_SIZE, THREAD_COUNT);
    // 初始化数据发送关系
    for (int i = 0; i < THREAD_COUNT; i++)
    {
        manager.connect(i, (i + 1) % THREAD_COUNT);
    }
    qDebug() << "machine max thread:" << QThreadPool::globalInstance()->maxThreadCount();

    QThreadPool::globalInstance()->setMaxThreadCount(THREAD_COUNT);
    qDebug() << "set max thread:" << THREAD_COUNT;

    qDebug() << "start task...";
    for (int i = 0; i < THREAD_COUNT; i++)
    {
        FastBufferHandler *handler = manager.bufferHandler(i);
        FastTask *task = new FastTask(handler);
        QThreadPool::globalInstance()->start(task);
    }

    // 等待所有任务完成
    bool rst = QThreadPool::globalInstance()->waitForDone();
    qDebug() << "timecount recording...";
    manager.printTimeCountLog();
    qDebug() << "success";
    std::string line;
    std::cout << "Press Enter to exit.";
    std::getline(std::cin, line);
    // return a.exec();
    return 0;
}
